OPC Studio User's Guide and Reference
Examples - OPC Data Access - Subscribe to items of various data types

.NET

// Shows how different data types can be subscribed to, including rare types and arrays of values.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using OpcLabs.EasyOpc.DataAccess;
using OpcLabs.EasyOpc.DataAccess.OperationModel;

namespace DocExamples.DataAccess._EasyDAClient
{
    partial class SubscribeMultipleItems
    {
        static void client_ItemChanged(object sender, EasyDAItemChangedEventArgs e)
        {
            Console.WriteLine();
            Console.WriteLine("Arguments.ItemDescriptor.ItemId: {0}", e.Arguments.ItemDescriptor.ItemId);

            if (e.Succeeded)
            {
                Debug.Assert(e.Vtq != null);
                Console.WriteLine("Vtq: {0}", e.Vtq);
            }
            else
                Console.WriteLine("*** Failure: {0}", e.ErrorMessageBrief);
        }

        public static void DataTypes()
        {
            IEnumerable<DAItemGroupArguments> arguments = new[]
                {
                    "Simulation.Register_EMPTY",
                    "Simulation.Register_NULL",
                    "Simulation.Register_DISPATCH",

                    "Simulation.ReadValue_I2",
                    "Simulation.ReadValue_I4",
                    "Simulation.ReadValue_R4",
                    "Simulation.ReadValue_R8",
                    "Simulation.ReadValue_CY",
                    "Simulation.ReadValue_DATE",
                    "Simulation.ReadValue_BSTR",
                    "Simulation.ReadValue_BOOL",
                    "Simulation.ReadValue_DECIMAL",
                    "Simulation.ReadValue_I1",
                    "Simulation.ReadValue_UI1",
                    "Simulation.ReadValue_UI2",
                    "Simulation.ReadValue_UI4",
                    "Simulation.ReadValue_INT",
                    "Simulation.ReadValue_UINT",

                    "Simulation.ReadValue_ArrayOfI2",
                    "Simulation.ReadValue_ArrayOfI4",
                    "Simulation.ReadValue_ArrayOfR4",
                    "Simulation.ReadValue_ArrayOfR8",
                    "Simulation.ReadValue_ArrayOfCY",
                    "Simulation.ReadValue_ArrayOfDATE",
                    "Simulation.ReadValue_ArrayOfBSTR",
                    "Simulation.ReadValue_ArrayOfBOOL",
                    //"Simulation.ReadValue_ArrayOfDECIMAL",
                    "Simulation.ReadValue_ArrayOfI1",
                    "Simulation.ReadValue_ArrayOfUI1",
                    "Simulation.ReadValue_ArrayOfUI2",
                    "Simulation.ReadValue_ArrayOfUI4",
                    "Simulation.ReadValue_ArrayOfINT",
                    "Simulation.ReadValue_ArrayOfUINT",
                }.Select(itemId => new DAItemGroupArguments("", "OPCLabs.KitServer.2", itemId, 3 * 1000, null));

            // Instantiate the client object.
            var client = new EasyDAClient();
            client.ItemChanged += client_ItemChanged;

            Console.WriteLine("Subscribing items...");
            client.SubscribeMultipleItems(arguments.ToArray());
            Thread.Sleep(30 * 1000);
            client.UnsubscribeAllItems();
            client.ItemChanged -= client_ItemChanged;
        }
    }
}
' Shows how different data types can be subscribed to, including rare types and arrays of values.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
' OPC client and subscriber examples in VB.NET on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VBNET .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.

Imports System.Threading
Imports OpcLabs.EasyOpc.DataAccess
Imports OpcLabs.EasyOpc.DataAccess.OperationModel

Namespace DataAccess._EasyDAClient
    Partial Friend Class SubscribeMultipleItems
        Private Shared Sub client_ItemChanged(sender As Object, e As EasyDAItemChangedEventArgs)

            Console.WriteLine()
            Console.WriteLine("ItemDescriptor.Arguments.ItemId: {0}", e.Arguments.ItemDescriptor.ItemId)
            If e.Succeeded Then
                Debug.Assert(e.Vtq IsNot Nothing)
                Console.WriteLine("Vtq: {0}", e.Vtq)
            Else
                Console.WriteLine("*** Failure: {0}", e.ErrorMessageBrief)
            End If
        End Sub

        Shared Sub DataTypes()
            Dim arguments As IEnumerable(Of DAItemGroupArguments) = New String() _
            { _
                "Simulation.Register_EMPTY",
                "Simulation.Register_NULL",
                "Simulation.Register_DISPATCH",
 _
                "Simulation.ReadValue_I2",
                "Simulation.ReadValue_I4",
                "Simulation.ReadValue_R4",
                "Simulation.ReadValue_R8",
                "Simulation.ReadValue_CY",
                "Simulation.ReadValue_DATE",
                "Simulation.ReadValue_BSTR",
                "Simulation.ReadValue_BOOL",
                "Simulation.ReadValue_DECIMAL",
                "Simulation.ReadValue_I1",
                "Simulation.ReadValue_UI1",
                "Simulation.ReadValue_UI2",
                "Simulation.ReadValue_UI4",
                "Simulation.ReadValue_INT",
                "Simulation.ReadValue_UINT",
 _
                "Simulation.ReadValue_ArrayOfI2",
                "Simulation.ReadValue_ArrayOfI4",
                "Simulation.ReadValue_ArrayOfR4",
                "Simulation.ReadValue_ArrayOfR8",
                "Simulation.ReadValue_ArrayOfCY",
                "Simulation.ReadValue_ArrayOfDATE",
                "Simulation.ReadValue_ArrayOfBSTR",
                "Simulation.ReadValue_ArrayOfBOOL",
                "Simulation.ReadValue_ArrayOfI1",
                "Simulation.ReadValue_ArrayOfUI1",
                "Simulation.ReadValue_ArrayOfUI2",
                "Simulation.ReadValue_ArrayOfUI4",
                "Simulation.ReadValue_ArrayOfINT",
                "Simulation.ReadValue_ArrayOfUINT"
            } _
            .Select(Function(itemId) New DAItemGroupArguments("", "OPCLabs.KitServer.2", itemId, 3 * 1000, Nothing))

            Console.WriteLine()

            Dim client As New EasyDAClient()
            Dim eventHandler = New EasyDAItemChangedEventHandler(AddressOf client_ItemChanged)
            AddHandler client.ItemChanged, eventHandler

            Console.WriteLine("Subscribing items...")
            client.SubscribeMultipleItems(arguments.ToArray())
            Thread.Sleep(30 * 1000)
            client.UnsubscribeAllItems()
            RemoveHandler client.ItemChanged, eventHandler
        End Sub
    End Class
End Namespace

Python

# Shows how different data types can be subscribed to, including rare types and arrays of values.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client and subscriber examples in Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python .
# Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
# a commercial license in order to use Online Forums, and we reply to every post.
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc
import time

# Import .NET namespaces.
from OpcLabs.EasyOpc.DataAccess import *
from OpcLabs.EasyOpc.DataAccess.OperationModel import *


# Item changed event handler.
def itemChanged(sender, e):
    print()
    print('Arguments.ItemDescriptor.ItemId: ', e.Arguments.ItemDescriptor.ItemId, sep='')

    if e.Succeeded:
        assert e.Vtq is not None
        print('Vtq: ', e.Vtq, sep='')
    else:
        print(' *** Failure: ', e.ErrorMessageBrief, sep='')


#
itemIds = [
    'Simulation.Register_EMPTY',
    'Simulation.Register_NULL',
    'Simulation.Register_DISPATCH',

    'Simulation.ReadValue_I2',
    'Simulation.ReadValue_I4',
    'Simulation.ReadValue_R4',
    'Simulation.ReadValue_R8',
    'Simulation.ReadValue_CY',
    'Simulation.ReadValue_DATE',
    'Simulation.ReadValue_BSTR',
    'Simulation.ReadValue_BOOL',
    'Simulation.ReadValue_DECIMAL',
    'Simulation.ReadValue_I1',
    'Simulation.ReadValue_UI1',
    'Simulation.ReadValue_UI2',
    'Simulation.ReadValue_UI4',
    'Simulation.ReadValue_INT',
    'Simulation.ReadValue_UINT',

    'Simulation.ReadValue_ArrayOfI2',
    'Simulation.ReadValue_ArrayOfI4',
    'Simulation.ReadValue_ArrayOfR4',
    'Simulation.ReadValue_ArrayOfR8',
    'Simulation.ReadValue_ArrayOfCY',
    'Simulation.ReadValue_ArrayOfDATE',
    'Simulation.ReadValue_ArrayOfBSTR',
    'Simulation.ReadValue_ArrayOfBOOL',
    # 'Simulation.ReadValue_ArrayOfDECIMAL',
    'Simulation.ReadValue_ArrayOfI1',
    'Simulation.ReadValue_ArrayOfUI1',
    'Simulation.ReadValue_ArrayOfUI2',
    'Simulation.ReadValue_ArrayOfUI4',
    'Simulation.ReadValue_ArrayOfINT',
    'Simulation.ReadValue_ArrayOfUINT',
]
arguments = map(lambda itemId: DAItemGroupArguments('', 'OPCLabs.KitServer.2', itemId, 3*1000, None), itemIds)

# Instantiate the client object.
client = EasyDAClient()
client.ItemChanged += itemChanged

print('Subscribing item changes...')
IEasyDAClientExtension.SubscribeMultipleItems(client, arguments)

print('Processing item change notifications for 30 seconds...')
time.sleep(30)

print('Unsubscribing all items...')
client.UnsubscribeAllItems()

client.ItemChanged -= itemChanged
print('Finished.')

 

See Also

Concepts